{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import csv as csv\n",
      "import numpy as np\n",
      "\n",
      "# Open up the csv file in to a Python object\n",
      "csv_file_object = csv.reader(open('train.csv', 'rb'))\n",
      "header = csv_file_object.next()\n",
      "\n",
      "data = []\n",
      "for row in csv_file_object:\n",
      "    data.append(row)\n",
      "data = np.array(data)\n",
      "\n",
      "# print data[0, 3]\n",
      "\n",
      "number_passengers = np.size(data[0::, 1].astype(np.float))\n",
      "number_survived = np.sum(data[0::,1].astype(np.float))\n",
      "proportion_survivors = number_survived / number_passengers\n",
      "\n",
      "print proportion_survivors\n",
      "\n",
      "women_only_stats = data[0::, 4] == 'female'\n",
      "# print 'women_only_stats: ', women_only_stats\n",
      "men_only_stats = data[0::, 4] != 'female'\n",
      "# print 'men_only_stats: ', men_only_stats\n",
      "\n",
      "women_onboard = data[women_only_stats, 1].astype(np.float)\n",
      "# print women_onboard\n",
      "men_onboard = data[men_only_stats, 1].astype(np.float)\n",
      "\n",
      "proportion_women_survived = np.sum(women_onboard) / np.size(women_onboard)\n",
      "proportion_men_survived = np.sum(men_onboard) / np.size(men_onboard)\n",
      "\n",
      "print 'Proportion of women who survived is %s' % proportion_women_survived\n",
      "print 'Proportion of men who survived is %s' % proportion_men_survived\n"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0.383838383838\n",
        "Proportion of women who survived is 0.742038216561\n",
        "Proportion of men who survived is 0.188908145581\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "test_file = open('test.csv', 'rb')\n",
      "test_file_object = csv.reader(test_file)\n",
      "header = test_file_object.next()\n",
      "\n",
      "prediction_file = open('genderbasedmodel.csv', 'wb')\n",
      "prediction_file_object = csv.writer(prediction_file)\n",
      "\n",
      "prediction_file_object.writerow(['PassengerId', 'Survived'])\n",
      "for row in test_file_object:\n",
      "    if row[3] == 'female':\n",
      "        prediction_file_object.writerow([row[0], '1'])\n",
      "    else:\n",
      "        prediction_file_object.writerow([row[0], '0'])\n",
      "test_file.close()\n",
      "prediction_file.close()"
     ],
     "language": "python",
     "outputs": [],
     "prompt_number": 30
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import csv as csv\n",
      "import numpy as np\n",
      "\n",
      "# Open up the csv file in to a Python object\n",
      "csv_file_object = csv.reader(open('train.csv', 'rb'))\n",
      "header = csv_file_object.next()\n",
      "\n",
      "data = []\n",
      "for row in csv_file_object:\n",
      "    data.append(row)\n",
      "data = np.array(data)\n",
      "\n",
      "fare_ceiling = 40\n",
      "data[data[0::,9].astype(np.float) >= fare_ceiling, 9] = fare_ceiling - 1.0\n",
      "\n",
      "fare_bracket_size = 10\n",
      "number_of_price_branckets = fare_ceiling / fare_bracket_size\n",
      "\n",
      "number_of_class = len(np.unique(data[0::,2]))\n",
      "# print number_of_class\n",
      "\n",
      "survival_table = np.zeros((2, number_of_class, number_of_price_branckets))\n",
      "# print survival_table\n",
      "\n",
      "for i in xrange(number_of_class):\n",
      "    for j in xrange(number_of_price_branckets):\n",
      "        women_only_stats = data[\n",
      "            (data[0::,4] == 'female') &\n",
      "            (data[0::,2].astype(np.float) == i+1) &\n",
      "            (data[0::,9].astype(np.float) >= j * fare_bracket_size) &\n",
      "            (data[0::,9].astype(np.float) < (j+1) * fare_bracket_size)\n",
      "        , 1]\n",
      "\n",
      "        men_only_stats = data[\n",
      "            (data[0::,4] != 'female') &\n",
      "            (data[0::,2].astype(np.float) == i+1) &\n",
      "            (data[0::,9].astype(np.float) >= j * fare_bracket_size) &\n",
      "            (data[0::,9].astype(np.float) < (j+1) * fare_bracket_size)\n",
      "        , 1]\n",
      "\n",
      "        survival_table[0, i, j] = np.mean(women_only_stats.astype(np.float))\n",
      "        survival_table[1, i, j] = np.mean(men_only_stats.astype(np.float))\n",
      "\n",
      "survival_table[survival_table != survival_table] = 0\n",
      "\n",
      "survival_table[survival_table < 0.5] = 0\n",
      "survival_table[survival_table >= 0.5] = 1\n",
      "\n",
      "test_file = open('test.csv', 'rb')\n",
      "test_file_object = csv.reader(test_file)\n",
      "header = test_file_object.next()\n",
      "prediction_file = open('genderclassmodel.csv', 'wb')\n",
      "p = csv.writer(prediction_file)\n",
      "p.writerow(['PassengerId', 'Survived'])\n",
      "\n",
      "for row in test_file_object:\n",
      "    for j in xrange(number_of_price_branckets):\n",
      "        try:\n",
      "            row[8] = float(row[8])\n",
      "        except:\n",
      "            bin_fare = 3 - float(row[1])\n",
      "            break\n",
      "        if row[8] > fare_ceiling:\n",
      "            bin_fare = number_of_price_branckets - 1\n",
      "            break\n",
      "        if (row[8] >= j * fare_bracket_size) and ((j+1) * fare_bracket_size):\n",
      "            bin_fare = j\n",
      "            break\n",
      "\n",
      "    if row[3] == 'female':\n",
      "        p.writerow([row[0], '%d' % int(survival_table[0, float(row[1])-1, bin_fare])])\n",
      "    else:\n",
      "        p.writerow([row[0], '%d' % int(survival_table[1, float(row[1])-1, bin_fare])])\n",
      "\n",
      "test_file.close()\n",
      "prediction_file.close()\n",
      "        "
     ],
     "language": "python",
     "outputs": [],
     "prompt_number": 95
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import csv\n",
      "import numpy as np\n",
      "\n",
      "csv_file_object = csv.reader(open('train.csv', 'rb'))\n",
      "header = csv_file_object.next()\n",
      "data = []\n",
      "\n",
      "for row in csv_file_object:\n",
      "    data.append(row)\n",
      "data = np.array(data)\n",
      "type(data[0::,5])\n",
      "ages_onboard = data[0::, 5].astype(np.float)"
     ],
     "language": "python",
     "outputs": [
      {
       "ename": "ValueError",
       "evalue": "could not convert string to float: ",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-1-aa6329835de6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mages_onboard\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mValueError\u001b[0m: could not convert string to float: "
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "import numpy as np\n",
      "import pylab as P\n",
      "\n",
      "df = pd.read_csv('train.csv', header=0)\n",
      "\n",
      "# for i in range(1,4):\n",
      "#     print i, len(df[(df[\"Sex\"] == 'male') & (df[\"Pclass\"] == i)])\n",
      "\n",
      "# df.Age.dropna().hist(bins=16, range=(0,80), alpha=0.5)\n",
      "# P.show()\n",
      "\n",
      "df[\"Gender\"] = df[\"Sex\"].map({'female': 0, 'male': 1}).astype(int)\n",
      "\n",
      "median_ages = np.zeros((2,3))\n",
      "\n",
      "for i in range(2):\n",
      "    for j in range(3):\n",
      "        median_ages[i,j] = df[(df[\"Gender\"] == i) & (df[\"Pclass\"] == j + 1)][\"Age\"].dropna().median()\n",
      "\n",
      "df[\"AgeFill\"] = df[\"Age\"]\n",
      "\n",
      "for i in range(2):\n",
      "    for j in range(3):\n",
      "        df.loc[(df.Age.isnull()) & (df.Gender == i) & (df.Pclass == j+1), \"AgeFill\"] = median_ages[i, j]\n",
      "\n",
      "df[\"AgeIsNull\"] = pd.isnull(df.Age).astype(int)\n",
      "\n",
      "df[\"FamilySize\"] = df['SibSp'] + df['Parch']\n",
      "\n",
      "df[\"Age*Class\"] = df.AgeFill * df.Pclass\n",
      "\n",
      "df = df.drop(['PassengerId', 'Name', 'Sex', 'Ticket', 'Cabin', 'Embarked', \"Age\"], axis=1)\n",
      "\n",
      "# print df[[\"Gender\", \"Pclass\", \"AgeFill\", \"AgeIsNull\", \"FamilySize\"]].head(10)\n",
      "\n",
      "# print df.head()\n",
      "train_data = df.values\n",
      "# train_data\n",
      "\n",
      "from sklearn.ense\n"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 41,
       "text": [
        "array([[  0. ,   3. ,   1. , ...,   0. ,   1. ,  66. ],\n",
        "       [  1. ,   1. ,   1. , ...,   0. ,   1. ,  38. ],\n",
        "       [  1. ,   3. ,   0. , ...,   0. ,   0. ,  78. ],\n",
        "       ..., \n",
        "       [  0. ,   3. ,   1. , ...,   1. ,   3. ,  64.5],\n",
        "       [  1. ,   1. ,   0. , ...,   0. ,   0. ,  26. ],\n",
        "       [  0. ,   3. ,   0. , ...,   0. ,   0. ,  96. ]])"
       ]
      }
     ],
     "prompt_number": 41
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df[\"AgeFill\"].dropna().hist()\n",
      "P.show()\n"
     ],
     "language": "python",
     "outputs": [],
     "prompt_number": 25
    }
   ]
  }
 ]
}